package br.edu.ifrn.sgpapp.activity;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import br.edu.ifrn.sgp.dtos.ProcessoDTO;
import br.edu.ifrn.sgpapp.R;
import br.edu.ifrn.sgpapp.adapter.ProcessosListAdapter;
import br.edu.ifrn.sgpapp.rest.ProcessoREST;

public class ListarProcessosActivity extends GenericActivity<List<ProcessoDTO>> {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lista_geral);
		processarSolicitacao();
	}
	
	@Override
	public List<ProcessoDTO> doInBackgroundTask(String... params) {
		return ProcessoREST.listarProcessosUsuario(getUsuarioLogado().getId());
	}

	@Override
	public void onPostExecuteTask(List<ProcessoDTO> processos) {
		final ListView listaProcessos = (ListView) findViewById(R.id.list);
		ProcessosListAdapter adapter = new ProcessosListAdapter(this, processos);
		listaProcessos.setAdapter(adapter);
		listaProcessos.setEmptyView(findViewById(R.id.textEmptyView));
		
		listaProcessos.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(ListarProcessosActivity.this, VerProcessoActivity.class);
				intent.putExtra(getString(R.string.extra_processo), (ProcessoDTO) listaProcessos.getAdapter().getItem(position));
				startActivity(intent);
			}
		});
	}
}
